<?php
namespace app\houtai\controller\user\agent;
use app\houtai\controller\BaseController;
use think\db\Expression;

class TypeController extends BaseController {
  /**
   *  @brief  : 代理地区查询
   *  @return : page
   *  @details: 
   */
  public function indexAction() {
    $conds = [];
    $order = [];
    ////////////////////////////////////////////////////////////////////////////
    $list = table('AgentType', 'rydb.websms')->where($conds)->order($order)->paginate();
    ////////////////////////////////////////////////////////////////////////////
    return $this->fetch('', [
      'list'  => $list,
    ]);
  }
  /**
   *  @brief  : 详情
   *  @return : page|JSON
   *  @details:
   */
  public function detailAction() {
    $id = $this->request->param('id/d', 0);
    if ($this->request->isGet()) {
      $data = [];
      if ($id > 0) {
        $data = table('AgentType', 'rydb.websms')->where('AgentType', $id)->find();
      }
      return $this->fetch('', ['data' => $data]);
    }
    if ($this->request->isPost()) {
      $name = $this->request->param('name/s', '', 'trim');
      if (empty($name) || $id < 0) {
        return result([], 1, '业务参数非法');
      }
      if ($id == 0) { // 添加
        $maxid = table('AgentType', 'rydb.websms')->max('AgentType');
        table('AgentType', 'rydb.websms')->insert(['AgentType'=>$maxid+1, 'AgentTypeName'=>$name]);
      } else { // 修改
        table('AgentType', 'rydb.websms')->where('AgentType', $id)->setField('AgentTypeName', $name);
      }
      return result([], 0);
    }
  }
  /**
   *  @brief  : 删除代理地区
   *  @return : JSON
   *  @details: 
   */
  public function deleteAction() {
    if (!$this->request->isPost()) {
      return result([], 100, '非法请求');
    }
    $id = $this->request->param('id/d', 0);
    if ($id < 0) {
      return result([], 101, '业务参数非法');
    }
    table('AgentReg', 'rydb.websms')->where('AgentType', $id)->setField('AgentType', 0);
    table('AgentType', 'rydb.websms')->where('AgentType', $id)->delete();
    return result([], 0);
  }
}
